package com.dsbj.icardservice.controller;

import com.dsbj.common.model.EmployeeV1;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.icardservice.entity.Dept;
import com.dsbj.icardservice.entity.Employee;
import com.dsbj.icardservice.service.EmployeeService;
import com.dsbj.icardservice.utils.strprocess.ObjectParse;
import com.dsbj.icardservice.utils.strprocess.StrUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author cjw 2019-09-24
 */
@RestController
@RequestMapping(value = "/employee")
public class EmployeeController extends BaseController {
	
	@Resource
	private EmployeeService employeeService;
	
	
	/**
	 * 根据employeeno查询employee信息（完全匹配查询）。
	 * tip1：传入参数: companyno(可为null),employeeno。
	 *
	 * @param
	 * @return
	 * @author cjw 2019-09-23
	 */
	@RequestMapping(value = "/getEmployeeByNo", method = RequestMethod.POST)
	public ResponseMessage getEmployeeByNo(@RequestBody Map<String, Object> map) {
		//解析传来的参数
		String employeeno = null;
		String companyno = null;
		try {
			companyno = ObjectParse.parse_toStr(map.get("companyno"));
			employeeno = ObjectParse.parse_toStr_elseException(map.get("employeeno"));
		} catch (Exception e) {
			return ResponseMessage.error("传入的参数有误!");
		}
		
		/**
		 * 根据employeeno查询employee信息。
		 */
		//Employee employee = employeeService.getEmployeeByNo(companyno, employeeno);
		Employee employee=employeeService.getEmployeeByNo(employeeno);
		return ResponseMessage.ok(employee);
	}
	
	/**
	 * 根据手机号(phoneNum)查询employee信息。
	 * tip1: 传入参数 phoneNum。
	 * @param
	 * @return
	 * @author cjw 2019-09-23
	 */
	@RequestMapping(value = "/getEmployeeByPhoneNum", method = RequestMethod.POST)
	public ResponseMessage getEmployeeByPhoneNum(@RequestBody Map<String, Object> map) {
		//解析传来的参数
		String phoneNum=null;
		try{
			phoneNum=ObjectParse.parse_toStr_elseException(map.get("phoneNum"));
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!");
		}
		
		/**
		 * 根据手机号(phoneNum)查询employee信息。
		 */
		return employeeService.getEmployeeByPhoneNum(phoneNum);
	}
	
	/**
	 * 根据employeename模糊查询employee信息。
	 * <p>
	 * tip1：传入参数: companyno,employeename。
	 * tip2：employeename中亦可传入employeeno信息。
	 *
	 * @param
	 * @return
	 * @author cjw 2019-09-23
	 */
	@RequestMapping(value = "/getEmployeeByNameDim", method = RequestMethod.POST)
	public ResponseMessage getEmployeeByNameDim(@RequestBody Map<String, Object> map) {
		//解析传来的参数
		String companyno = null;
		String employeenameDim = null;
		try {
			companyno = ObjectParse.parse_toStr_elseException(map.get("companyno"));
			employeenameDim = ObjectParse.parse_toStr_elseException(map.get("employeename"));
		} catch (Exception e) {
			return ResponseMessage.error("传入的参数有误!");
		}
		
		/**
		 * 根据employeename模糊查询employee信息。
		 */
		
		//分析employeenameDim是name还是no
		String emplNameDim_real = null;
		String emplNoDim_real = null;
		boolean hasDigit = StrUtil.hasDigit(employeenameDim);
		
		/**
		 * 若hasDigit为true, 为no
		 */
		if (hasDigit == true) {
			emplNoDim_real = employeenameDim;
			
			//若emplNoDim.length<5位，不查询
			if (emplNoDim_real.length() < 5) {
				return ResponseMessage.ok();
			}
			
			List<Employee> employees = employeeService.getEmplByEmployeenoDim(companyno, emplNoDim_real);
			return ResponseMessage.ok(
					employees.stream().map(
							x -> new EmployeeV1(x.getId(), x.getCompanyno(), x.getEmployeeno(), x.getEmployeename())
					).collect(Collectors.toList()));
		} else {
			/**
			 * 若hasDigit为false，为no
			 */
			emplNameDim_real = employeenameDim;
			
			//若emplNameDim_real长度<2，不进行查询。
			if (emplNameDim_real.length() < 2) {
				return ResponseMessage.ok();
			}
			
			List<Employee> employees = employeeService.getEmplByEmployeenameDim(companyno, emplNameDim_real);
			return ResponseMessage.ok(
					employees.stream().map(
							x -> new EmployeeV1(x.getId(), x.getCompanyno(), x.getEmployeeno(), x.getEmployeename())
					).collect(Collectors.toList()));
		}
	}
	
	
	
	
	
	
}
